package cn.lkp.mall.ware.service.impl;

import cn.lkp.common.utils.R;
import cn.lkp.mall.ware.feign.MemberFeign;
import cn.lkp.mall.ware.vo.FareVo;
import cn.lkp.mall.ware.vo.MemberAddressVo;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lkp.common.utils.PageUtils;
import cn.lkp.common.utils.Query;

import cn.lkp.mall.ware.dao.WareInfoDao;
import cn.lkp.mall.ware.entity.WareInfoEntity;
import cn.lkp.mall.ware.service.WareInfoService;
import org.springframework.util.StringUtils;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
    @Autowired
    MemberFeign memberFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                new QueryWrapper<WareInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("id",key).or().like("name",key).or().like("address",key).or().like("areacode",key);
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        //收货地址的详细信息
        R info = memberFeign.info(addrId);
        MemberAddressVo memberAddressVo = info.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {
        });
        if(memberAddressVo!=null){
            String phone = memberAddressVo.getPhone();
            //截取用户手机号码最后一位作为我们的运费计算
            //13580262051
            String fare = phone.substring(phone.length() - 1, phone.length());
            BigDecimal bigDecimal = new BigDecimal(fare);
            fareVo.setAddress(memberAddressVo);
            fareVo.setFare(bigDecimal);
            return fareVo;
        }
        return null;
    }
    public static void main(String[] args) {
        String phone = "10245678901";
        String fare = phone.substring(phone.length() - 1, phone.length());
        System.out.println(fare);
    }

}